﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.Core.Modelling.Primitives;
using System.Drawing;
using ITAP.Core.Modelling;


namespace ITAP.ContourBuilder.Components.Dimensions
{
    public enum LeaderDimensionType
    {
        Horizontal,
        Vertical
    }


    [Serializable]
    public class ElementLeaderDimension : ContourElementDimension
    {
        public override string DimensionText
        {
            get
            {
                if (this.leaderType == LeaderDimensionType.Horizontal)
                {
                    if (this.PrefixSymbol != String.Empty)
                        return this.PrefixSymbol + " = " + this.Primitive.BoundingBox.Height.ToString("0.0");
                    else
                        return this.Primitive.BoundingBox.Height.ToString("0.0");
                }
                else
                {
                    if (this.PrefixSymbol != String.Empty)
                        return this.PrefixSymbol + " = " + this.Primitive.BoundingBox.Width.ToString("0.0");
                    else
                        return this.Primitive.BoundingBox.Width.ToString("0.0");
                }
            }
        }

        private LeaderDimensionType leaderType;

        public LeaderDimensionType LeaderType
        {
            get { return leaderType; }
            set { leaderType = value; }
        }

        public override void Recalculate()
        {
            if (this.Element == null)
                return;

            RectangleF bbox = this.Element.BoundingBox;
            Line line = null;

            PointF downPoint;
            PointF upPoint;
            PointF leftPoint;
            PointF rightPoint;

            switch (this.leaderType)
            {
                case LeaderDimensionType.Horizontal:
                    downPoint = new PointF((float)(this.Element.StartPoint.X + this.Element.EndPoint.X) / 2, (float)this.Element.StartPoint.Y);
                    upPoint = new PointF((float)(this.Element.StartPoint.X + this.Element.EndPoint.X) / 2, (float)this.Element.EndPoint.Y);
                    line = new Line(new ModelPoint(downPoint.X, downPoint.Y), new ModelPoint(upPoint.X, upPoint.Y));
                    break;
                case LeaderDimensionType.Vertical:
                    leftPoint = new PointF((float)this.Element.StartPoint.X, (float)(this.Element.EndPoint.Y + this.Element.StartPoint.Y) / 2);
                    rightPoint = new PointF((float)this.Element.EndPoint.X, (float)(this.Element.EndPoint.Y + this.Element.StartPoint.Y) / 2);
                    line = new Line(new ModelPoint(rightPoint.X, rightPoint.Y), new ModelPoint(leftPoint.X, leftPoint.Y));
                    break;
            }

            line = (Line)line.GetOffsetPrimitive(this.OffsetFromPrimitive);
            this.Primitive = line;
        }

        public override void AcceptDimensionVisitor(IDimensionVisitor visitor)
        {
            visitor.Visit(this);
        }
    }
}
